layui.use(['table', 'layer'], function () {
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;
    /**
     * 用户列表展示
     */

    var tableIns = table.render({
        elem: '#seekList',
        url: ctx + '/seek/querySeek',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limits: [10, 15, 20, 25],
        limit: 10,
        toolbar: "#toolbarDemo",
        id: "seekList",
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: "id", title: '编号', fixed: "true", width: 80},
            {field: 'title', title: '求购标题', minWidth: 50, align: "center"},
            {field: 'remark', title: '备注', minWidth: 100, align: 'center'},
            {field: 'minPrice', title: '最低价格', minWidth: 100, align: 'center'},
            {field: 'maxPrice', title: '最高价格', minWidth: 100, align: 'center'},
            {field: 'userName', title: '发布者', align: 'center'},
            {field: 'publishDate', title: '发布时间', align: 'center', minWidth: 150},
            {title: '操作', minWidth: 150, templet: '#seekListBar', fixed: "right", align: "center"}
        ]]
    });

    /*条件查询*/
    $(".search_btn").on("click", function () {
        table.reload("seekList", {
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                title: $("input[name='title']").val(),
                publishDate: $("input[name='publishDate']").val(),

            }
        })
    });


    /*绑定头部工具栏*/
    //触发事件
    table.on('toolbar(seek)', function (obj) {
        var checkStatus = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'add':
                openAddOrEdit();
                break;
            case 'ref':
                refresh();
                break;
        }
    });


    /*添加与查看求购信息*/
    function openAddOrEdit(id) {
        var title = "<h2>添加求购信息</h2>";
        var url = ctx + "/seek/add";
        console.log(id);
        //判断添加还是查看详情
        if(id){
            title="<h2>查看详情</h2>";
            url=url+"?id="+id;
        }
        layer.open({
            title: title,
            type: 2,//iframe
            content: url,
            maxmin: true,
            area: ["720px", "360px"]
        });
    }


    /*绑定行内工具栏*/
    table.on('tool(seek)', function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）

        if (layEvent === 'edit') { //查看
            //layer.msg("查看详情")
            openAddOrEdit(data.id);
        } else if (layEvent === 'del') { //删除
            layer.confirm('真的要删除么', function(index){
                //发送ajax删除数据
                $.post(ctx+"/seek/delete",{"seekId":data.id},function(res){
                    //判断
                    if(res.code == 200){
                        layer.close(index);
                        //刷新
                        tableIns.reload();
                    }else{
                        //提示一下
                        layer.msg(res.msg,{icon:5 });
                    }
                },"json");
            });
        }
    });


    /*刷新*/
    function refresh(data) {
        parent.location.reload();
    }

});